from datetime import datetime

from fastapi import APIRouter, Depends, Request
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from pydantic_validation_decorator import ValidateFields

from module_admin.entity.do.rule_do import RuleMonitor
from module_admin.entity.vo.rule_vo import RulePageQueryModel, RuleModel
from module_admin.service.login_service import LoginService
from module_admin.service.rule_service import RuleService
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil
from module_admin.entity.vo.user_vo import CurrentUserModel

ruleController = APIRouter(prefix='/rule/monitor', dependencies=[Depends(LoginService.get_current_user)])


@ruleController.get(
    '/list',

    response_model=PageResponseModel,
    dependencies=[Depends(CheckUserInterfaceAuth('rule:monitor:list'))]
)
async def get_rule_monitor_list(
    request: Request,
    rule_query_model:RulePageQueryModel  = Depends(RulePageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    rule_query_result = await RuleService.get_rule_list_service(query_db, rule_query_model,is_page=True)
    logger.info('获取成功')
    return ResponseUtil.success(
        model_content=PageResponseModel(rows=rule_query_result.rows, total=len(rule_query_result.rows))
    )

@ruleController.get(
    '/{rule_id}', response_model=RuleModel, dependencies=[Depends(CheckUserInterfaceAuth('rule:monitor:query'))]
)
async def query_detail_rule_monitor(request: Request, rule_id: int, query_db: AsyncSession = Depends(get_db)):
    rule_detail_result = await RuleService.rule_detail_services(query_db, rule_id)
    logger.info(f'获取rule_id为{rule_id}的信息成功')

    return ResponseUtil.success(data=rule_detail_result)


@ruleController.post('', dependencies=[Depends(CheckUserInterfaceAuth('rule:monitor:add'))])
@ValidateFields(validate_model='add_rule')
@Log(title='规则监控', business_type=BusinessType.INSERT)
async def add_rule_monitor(
    request: Request,
    rule_model: RuleModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    rule_model.create_by = current_user.user.user_name
    rule_model.update_by = current_user.user.user_name
    rule_model.create_time = datetime.now()
    rule_model.update_time = datetime.now()
    add_rule_result = await RuleService.add_rule_monitor_service(query_db, rule_model)
    logger.info(add_rule_result.message)

    return ResponseUtil.success(msg=add_rule_result.message)


@ruleController.put('', dependencies=[Depends(CheckUserInterfaceAuth('rule:monitor:edit'))])
@ValidateFields(validate_model='edit_rule')
@Log(title='规则监控', business_type=BusinessType.UPDATE)
async def edit_rule_monitor(
    request: Request,
    rule_model: RuleModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    rule_model.update_by = current_user.user.user_name
    rule_model.update_time = datetime.now()
    edit_rule_result = await RuleService.edit_rule_monitor_service(query_db, rule_model)
    logger.info(edit_rule_result.message)

    return ResponseUtil.success(msg=edit_rule_result.message)


@ruleController.delete('/{rule_ids}', dependencies=[Depends(CheckUserInterfaceAuth('rule:monitor:remove'))])
@Log(title='规则监控', business_type=BusinessType.DELETE)
async def delete_rule_monitor(
    request: Request,
    rule_ids: str,
    query_db: AsyncSession = Depends(get_db),
):
    delete_rule_result = await RuleService.delete_rule_monitor_service(query_db, rule_ids)
    logger.info(delete_rule_result.message)

    return ResponseUtil.success(msg=delete_rule_result.message)

@ruleController.post('/changeStatus', dependencies=[Depends(CheckUserInterfaceAuth('rule:monitor:changeStatus'))])
@Log(title='规则监控', business_type=BusinessType.UPDATE)
async def changeStatus_rule_monitor(
    request: Request,
    rule_model: RuleModel,
    query_db: AsyncSession = Depends(get_db),
):
    delete_rule_result = await RuleService.changeStatus_rule_monitor_service(query_db, rule_model)
    logger.info(delete_rule_result.message)

    return ResponseUtil.success(msg=delete_rule_result.message)



